using System.Collections.Generic;
using System.ComponentModel;
using StockTraderRI.Modules.News.Model;

namespace StockTraderRI.Modules.News.Client
{
    public class NewsCache : INewsCache
    {
        private readonly Dictionary<string, BindingList<NewsArticle>> news =
            new Dictionary<string, BindingList<NewsArticle>>();

        public void AddNews(string tickerSymbol, NewsArticle article)
        {
            GetNewsForTicker(tickerSymbol).Add(article);
        }

        public BindingList<NewsArticle> GetNews(string tickerSymbol)
        {
            return GetNewsForTicker(tickerSymbol);
        }

        private BindingList<NewsArticle> GetNewsForTicker(string tickerSymbol)
        {
            if (!news.ContainsKey(tickerSymbol))
            {
                lock (news)
                {
                    if (!news.ContainsKey(tickerSymbol))
                    {
                        news[tickerSymbol] = new BindingList<NewsArticle>();
                    }
                }
            }

            return news[tickerSymbol];
        }
    }
}